//
//  MessageViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/6/24.
//  Copyright © 2016年 XuDong Jin. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageCell.h"
#import "MessageContentCell.h"
#import "PrivateMessageListVC.h"
#import "SystemDetaileViewController.h"
#import "FCommentListViewController.h"
#import "BlindApplyVC.h"
#import "SysMegListModel.h"
#import "IGMsgInfoApi.h"
#import "IGSysMsgListApi.h"
#import"IGHandleMsgApi.h"

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSString *bindnum;
    NSString *commeNum;
    NSString *simiNum;
    
}

@property (nonatomic, strong) UITableView *tabelView;
@property(nonatomic,strong) IGSysMsgListApi *msgListApi;
@property(nonatomic,strong) IGMsgInfoModel *msgModel;
@property(nonatomic,strong) IGHandleMsgApi *handleApi;
@property(nonatomic,strong) NSMutableArray *msgArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, copy) NSString *lastTimeDate;
@end

@implementation MessageViewController
#pragma mark -懒加载
-(NSMutableArray *)msgArray{
    if (!_msgArray) {
        _msgArray=[NSMutableArray array];
    }
    return _msgArray;
}
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = @"消息中心";
    self.title = NSLocalizedString(@"Message Center", nil);

    self.view.backgroundColor = [UIColor mainBackgroundColor];
    
    AdjustsScrollViewInsetNever(self, self.tabelView);
    [self.view addSubview:self.tabelView];
    [_tabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(NavigationMagin);
    }];
    
    [_tabelView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:@"MessageCell"];
    [_tabelView registerNib:[UINib nibWithNibName:@"MessageContentCell" bundle:nil] forCellReuseIdentifier:@"MessageContentCell"];
    _tabelView.tableFooterView = [UIView new];
    _page = 1;
    _lastTimeDate = @"";
    self.msgListApi = [IGSysMsgListApi new];
    __weak typeof(self) weakSelf = self;
    [self.tabelView ig_setHeaderWithRefreshingBlock:^{
        weakSelf.page = 1;
        weakSelf.lastTimeDate = @"";
        [weakSelf requestData];
    }];
}

- (UITableView *)tabelView {
    if (!_tabelView) {
        _tabelView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tabelView.delegate = self;
        _tabelView.dataSource = self;
        
        if (@available(iOS 11.0, *)) {
            _tabelView.estimatedRowHeight = 0;
            _tabelView.estimatedSectionHeaderHeight = 0;
            _tabelView.estimatedSectionFooterHeight = 0;
            _tabelView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tabelView;
}

-(void)requestData{
    _msgListApi.pageNum=_page;
    _msgListApi.lastTimeDate=_lastTimeDate;
    @KWeakObj(self)
    [self.msgListApi startWithDecodeClass:[IGSysMsgListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.tabelView ig_endHeaderRefreshing];
        [selfWeak.tabelView ig_endFooterRefreshing];
        if (responseObject) {
            IGSysMsgListResp *resp=responseObject;
            IGSysMsgListModel *model=resp.data;
            self.msgModel=model.nums;
            bindnum=_msgModel.bindNums;
            commeNum=_msgModel.commentNums;
            simiNum=_msgModel.imNums;
            if (model.sysMsgs&&model.sysMsgs.count>0) {
                [selfWeak setupFooter];
            }  else {
                [_tabelView ig_endFooterRefreshingWithNoMoreData];
            }
            _lastTimeDate = model.lastTimeDate;
            if (_page == 1) {
                _msgArray = [NSMutableArray arrayWithArray:model.sysMsgs];
            } else {
                [_msgArray addObjectsFromArray:model.sysMsgs];
            }
            [self.tabelView reloadData];
        }
        else
        {
            //空白页处理逻辑写在这里
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.tabelView ig_setFooterWithRefreshingBlock:^{
        selfWeak.page++;
        [selfWeak requestData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabelView ig_beginHeaderRefreshing];
}

#pragma mark - UITableViewDelegate UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }
    return _msgArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        MessageCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
//        NSArray *arr = @[@"绑定申请",@"评论我的", @"我评论的",@"私信"];
        NSArray *arr = @[NSLocalizedString(@"Link Request", nil),NSLocalizedString(@"Comments Received", nil),NSLocalizedString(@"Comments Given", nil),NSLocalizedString(@"Private Message", nil)];
        cell.str = arr[indexPath.row];
        if(indexPath.row==0){
            cell.numLab.text =self.msgModel.bindNums;
            if ([self.msgModel.bindNums integerValue] != 0) {
                cell.numLab.hidden=NO;
                cell.redImage.hidden=NO;
            } else {
                cell.numLab.hidden=YES;
                cell.redImage.hidden=YES;
            }
        } else if (indexPath.row ==1) {
            cell.numLab.text = self.msgModel.commentNums;
            if ([self.msgModel.commentNums integerValue] != 0) {
                cell.numLab.hidden=NO;
                cell.redImage.hidden=NO;
            } else {
                cell.numLab.hidden=YES;
                cell.redImage.hidden=YES;
            }
        } else if (indexPath.row==3){
            cell.numLab.text =_msgModel.imNums;
            if ([self.msgModel.imNums integerValue] != 0) {
                cell.numLab.hidden=NO;
                cell.redImage.hidden=NO;
            } else {
                cell.numLab.hidden=YES;
                cell.redImage.hidden=YES;
            }
        }
        return cell;
    } else if(indexPath.section==1){
        MessageContentCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MessageContentCell"];
        SysMegListModel *systemMessageModel = _msgArray[indexPath.row];
        cell.contentLab.text = systemMessageModel.title;
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            BlindApplyVC *vc=[[BlindApplyVC alloc]initWithNibName:@"BlindApplyVC" bundle:nil];
            [UIViewController ig_pushViewController:vc animated:YES];
        }else if (indexPath.row==1){
            FCommentListViewController  * vc = [[FCommentListViewController alloc]initWithNibName:@"FCommentListViewController" bundle:nil];
            vc.type = indexPath.row-1;
            [UIViewController ig_pushViewController:vc animated:YES];
        }else if (indexPath.row==2){
            FCommentListViewController  * vc = [[FCommentListViewController alloc]initWithNibName:@"FCommentListViewController" bundle:nil];
            vc.type = indexPath.row-1;
            [UIViewController ig_pushViewController:vc animated:YES];
        }else if (indexPath.row==3){
            PrivateMessageListVC * vc = [[PrivateMessageListVC alloc]initWithNibName:@"PrivateMessageListVC" bundle:nil];
            [UIViewController ig_pushViewController:vc animated:YES];
        }
    }
    if (indexPath.section == 1) {
        SystemDetaileViewController * vc = [[SystemDetaileViewController alloc]initWithNibName:@"SystemDetaileViewController" bundle:nil];
        SysMegListModel *systemMessage = _msgArray[indexPath.row];
        vc.msgModel = systemMessage;
        [UIViewController ig_pushViewController:vc animated:YES];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if(section == 1){
        UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,KSCREENWIDTH, 40)];
//        titleLab.text = [NSString stringWithFormat:@"  %@",@"系统消息"];
        titleLab.text = [NSString stringWithFormat:@"  %@",NSLocalizedString(@"System Message", nil)];

        titleLab.font=[UIFont systemFontOfSize:16];
        titleLab.backgroundColor = [UIColor clearColor];
        return titleLab;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 10.f;
    }
    if(section == 1){
        return 40.f;
    }
    
    return 10.f;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==0)
    {
        return UITableViewCellEditingStyleNone;
    }else
    {
        return UITableViewCellEditingStyleDelete;
    }
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    SysMegListModel * systemMessageModel = _msgArray[indexPath.row];
    self.handleApi = [IGHandleMsgApi new];
    self.handleApi.type = @"4";
    self.handleApi.msgId = systemMessageModel.mid;
    [self.handleApi startWithDecodeClass:[IGHandleMsgResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            // 删除模型
            [_msgArray removeObjectAtIndex:indexPath.row];
            // 刷新
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    return @"删除";
    return NSLocalizedString(@"Delete", nil);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
